
//
//  HDStatus.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/12.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit

class HDStatus: NSObject {
    //MARK - 
    //微博创建时间
    var created_at :String?
    //微博ID
    var id :Int64 = 0
    //微博信息内容
    var text :String?
    //微博来源
    var source :String? {
        didSet {
            if let  newSource = source{
                    let  firstRange = newSource.rangeOfString("\">")
                    let  secondRange = newSource.rangeOfString("</a>")
                guard let firstIndex = firstRange?.endIndex else{
                    return
                }
                guard let endIndex = secondRange?.startIndex else{
                    return
                }
                source = "来自" + newSource.substringWithRange(firstIndex..<endIndex)
            }
        }
    }
    //微博配图
    var pic_urls:[[String:AnyObject]]?{
        didSet{
            //当kvc给pic_urls赋值的时候  将pic_urls转换成NSURL数组方便外面的人使用
            //判断图片数量
            let  count = pic_urls?.count ?? 0
            //1.没有图片
            if count == 0 {
                return
            }
            //2.有图片
            //创建一个空数组来存储转化好url
            storePictureURLs = [NSURL]()
            for dict  in pic_urls!{
                //获取每个字典的中URLString
                let urlString = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlString)!
                storePictureURLs?.append(url)
            }
            
            
        }
    }
    //用户基本数据
    var user:HDUser?
    //转发数据模型
    var retweeted_status :HDStatus?
    //行高
    var cellHeight :CGFloat?
    
    //MARK -赋值属性   图片的url
    var storePictureURLs:[NSURL]?
    
    
    
    //kvc构造器 字典转模型
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //嵌套模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String:AnyObject]{
                user = HDUser(dict: dict)
            }
            return
        }
        //转化微博模型
        if  key == "retweeted_status" {
            if  let dict = value as?[String:AnyObject]{
                retweeted_status = HDStatus(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //对象的打印  模型转字典
    override var description:String {
        get{
            let keys = ["created_at","id","text","source","pic_urls","user"]
            return self.dictionaryWithValuesForKeys(keys).description
        }
    }
    
    

}
